package com.example.taobaounion.view.adapter;

import android.graphics.Paint;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.example.taobaounion.R;
import com.example.taobaounion.model.domain.OnSellContent;
import com.example.taobaounion.utils.UrlUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class OnSellContentAdapter extends RecyclerView.Adapter<OnSellContentAdapter.InnerHolder> {
    private List<OnSellContent.DataBean.TbkDgOptimusMaterialResponseBean.ResultListBean.MapDataBean> mData = new ArrayList<>();
    private OnSellPageItemClickListener mContentItemClickListener;

    @NonNull
    @Override
    public InnerHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_on_sell_content, parent, false);
        return new InnerHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull InnerHolder holder, int position) {
        OnSellContent.DataBean.TbkDgOptimusMaterialResponseBean.ResultListBean.MapDataBean mapDataBean = mData.get(position);

        holder.setData(mapDataBean);

        holder.itemView.setOnClickListener(v -> {
            mContentItemClickListener.OnSellItemClick(mapDataBean);
        });
    }

    @Override
    public int getItemCount() {
        return mData.size();
    }

    public void setData(OnSellContent content) {
        this.mData.clear();

        mData.addAll(content.getData().getTbk_dg_optimus_material_response().getResult_list().getMap_data());
    }

    public void onMoreLoaded(OnSellContent sellContent) {

        int oldSize = mData.size();
        List<OnSellContent.DataBean.TbkDgOptimusMaterialResponseBean.ResultListBean.MapDataBean> map_data = sellContent.getData().getTbk_dg_optimus_material_response().getResult_list().getMap_data();
        mData.addAll(map_data);

        notifyItemRangeChanged(oldSize - 1, mData.size());
    }

    static class InnerHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.on_sell_cover)
        public ImageView coverIv;

        @BindView(R.id.on_sell_content_title)
        public TextView contentTitleTv;

        @BindView(R.id.on_sell_original_price)
        public TextView originalPriceTv;

        @BindView(R.id.on_sell_off_price_tv)
        public TextView offPriceTv;

        InnerHolder(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }

        public void setData(OnSellContent.DataBean.TbkDgOptimusMaterialResponseBean.ResultListBean.MapDataBean mapDataBean) {
            contentTitleTv.setText(mapDataBean.getTitle());

            String coverPath = UrlUtils.getCoverPath(mapDataBean.getPict_url(), 200);

            Glide.with(coverIv.getContext()).load(coverPath).into(coverIv);

            String originalPrice = mapDataBean.getZk_final_price();
            originalPriceTv.setText(String.format("%.4s元 ", originalPrice));

            // 添加一个中划线
            originalPriceTv.setPaintFlags(Paint.STRIKE_THRU_TEXT_FLAG);

            float originalPriceF = Float.parseFloat(originalPrice);

            float finalPrice = originalPriceF - mapDataBean.getCoupon_amount();

            offPriceTv.setText(String.format("劵价格：%.4s", finalPrice));
        }
    }

    public void setOnSellPageItemClickListener(OnSellPageItemClickListener listener) {
        this.mContentItemClickListener = listener;
    }

    public interface OnSellPageItemClickListener {
        void OnSellItemClick(OnSellContent.DataBean.TbkDgOptimusMaterialResponseBean.ResultListBean.MapDataBean mapDataBean);
    }
}
